package weaver.interfaces.action;

import com.alibaba.fastjson.JSONObject;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2023/12/20
 * @Description 考核结果异常消息提醒
 * @Author AdminZm
 */
public class GradeAbnormalMessageAction implements Action {

    BaseBean baseBean = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        int billid = requestInfo.getRequestManager().getBillid();
        String requestId = requestInfo.getRequestid();
        String formtable = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        String detailCount = baseBean.getPropValue("ss_prop_zm", "grade_workflow_detail_count");
        if (StringUtils.isEmpty(detailCount)) {
            detailCount = "5";
        }
        try {

            // 分别获取C、D等级人数
            int c_dj = 0;
            int d_dj = 0;
            int dtCountInt = Integer.parseInt(detailCount);
            for (int i = 1; i <= dtCountInt; i++) {
                rs.executeQuery("SELECT count(xm) as cnt FROM " + formtable + "_dt" + i + " WHERE dj = 3 and mainid = ?", billid);
                while (rs.next()) {
                    int cnt = rs.getInt("cnt");
                    c_dj = c_dj + cnt;
                }
            }
            for (int i = 1; i <= dtCountInt; i++) {
                rs.executeQuery("SELECT count(xm) as cnt FROM " + formtable + "_dt" + i + " WHERE dj = 4 and mainid = ?", billid);
                while (rs.next()) {
                    int cnt = rs.getInt("cnt");
                    d_dj = d_dj + cnt;
                }
            }

            if (c_dj == 0 && d_dj == 0) {
                baseBean.writeLog("no person C or D.");
                return Action.SUCCESS;
            }

            String roleName = "绩效异常通知角色";
            Set<String> roleSet = new HashSet<>();
            rs.executeQuery("SELECT RESOURCEID FROM hrmrolemembers WHERE ROLEID = (SELECT ID FROM hrmroles WHERE ROLESMARK = ?)", roleName);
            while (rs.next()) {
                String id = rs.getString("RESOURCEID");
                roleSet.add(id);
            }
            if (!CollectionUtils.isEmpty(roleSet)) {
                String title = "绩效考核结果通知"; // 标题
                String context = "您好，本次绩效考核结果已完成，其中C级别" + c_dj + "人，D级别" + d_dj + "人。点击查看详情"; // 内容
                String linkUrl = "/spa/workflow/static4form/index.html?_rdm=1708328960238#/main/workflow/req?requestid=" + requestId; // PC端链接
                String linkMobileUrl = "/spa/workflow/static4form/index.html?_rdm=1708328960238#/main/workflow/req?requestid=" + requestId; // 移动端链接
                try {
                    String source = baseBean.getPropValue("ss_prop_zm", "grade_abnormal_msg_source");
                    if (StringUtils.isEmpty(source)) {
                        source = "523";
                    }
                    MessageType messageType = MessageType.newInstance(Integer.parseInt(source));
                    baseBean.writeLog("start to send msg," + requestId);
                    MessageBean messageBean = Util_Message.createMessage(messageType, roleSet, title, context, linkUrl, linkMobileUrl);
                    messageBean.setCreater(1);// 创建人id
                    Util_Message.store(messageBean);
                } catch (IOException e) {
                    baseBean.writeLog("GradeAbnormalMessageAction error:" + e.getMessage());
                }
            }

//            // 明细表中所有C/D的人员
//            Set<String> resoueceIds = new HashSet<>();
//            int dtCountInt = Integer.parseInt(detailCount);
//            for (int i = 1; i <= dtCountInt; i++) {
//                rs.executeQuery("SELECT xm FROM " + formtable + "_dt" + i + " WHERE (dj = 3 or dj = 4) and mainid = ?", billid);
//                while (rs.next()) {
//                    String xm = rs.getString("xm");
//                    resoueceIds.add(xm);
//                }
//            }
//            if (CollectionUtils.isEmpty(resoueceIds)) {
//                baseBean.writeLog("no person id C or D.");
//                return Action.SUCCESS;
//            }
//
//            String source = baseBean.getPropValue("ss_prop_zm", "grade_abnormal_msg_source");
//            if (StringUtils.isEmpty(source)) {
//                source = "523";
//            }
//            // 从人事审批矩阵获取function head
//            String matrixId = baseBean.getPropValue("ss_prop_zm", "hrm_sp_matrix_id");
//            if (StringUtils.isEmpty(matrixId)) {
//                matrixId = "4";
//            }
//            String functionheadSql = "SELECT functionalhead FROM matrixtable_" + matrixId + " WHERE bmmc = ?";
//            baseBean.writeLog("functionheadSql is:" + functionheadSql);
//
////        String roleName = baseBean.getPropValue("ss_prop_zm", "grade_abnormal_notice_role_name");
//            String roleName = "绩效异常通知角色";
//            Set<String> roleSet = new HashSet<>();
//            rs.executeQuery("SELECT RESOURCEID FROM hrmrolemembers WHERE ROLEID = (SELECT ID FROM hrmroles WHERE ROLESMARK = ?)", roleName);
//            while (rs.next()) {
//                String id = rs.getString("RESOURCEID");
//                roleSet.add(id);
//            }
//            for (String resourceId : resoueceIds) {
//                MessageType messageType = MessageType.newInstance(Integer.parseInt(source));
//                Set<String> userIdList = new HashSet<>(); // 接收人id 必填
//                // 获取人员所在部门与直接上级
//                rs.executeQuery("SELECT DEPARTMENTID, MANAGERID FROM hrmresource WHERE id = ?", resourceId);
//                int bmid = 0;
//                if (rs.next()) {
//                    bmid = rs.getInt("DEPARTMENTID");
//                    String manageId = rs.getString("MANAGERID");
//                    if (StringUtils.isNotEmpty(manageId) && !StringUtils.equals(manageId, "0")) {
//                        userIdList.add(manageId);
//                    }
//                }
//                if (bmid == 0) {
//                    continue;
//                }
//                rs.executeQuery(functionheadSql, bmid);
//                if (rs.next()) {
//                    String functionalhead = rs.getString("functionalhead");
//                    if (StringUtils.isNotEmpty(functionalhead)) {
//                        String[] fArr = functionalhead.split(",");
//                        userIdList.addAll(Arrays.asList(fArr));
//                    }
//                }
//                userIdList.addAll(roleSet);
//                if (CollectionUtils.isEmpty(userIdList)) {
//                    continue;
//                }
//                String title = "考核成绩异常提醒"; // 标题
//                String context = "考核成绩异常提醒"; // 内容
//                String linkUrl = "/spa/workflow/static4form/index.html?_rdm=1708328960238#/main/workflow/req?requestid="; // PC端链接
//                String linkMobileUrl = ""; // 移动端链接
//                try {
//                    baseBean.writeLog("start to send msg," + requestId);
//                    MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
//                    messageBean.setCreater(1);// 创建人id
//                    Util_Message.store(messageBean);
//                } catch (IOException e) {
//                    baseBean.writeLog("GradeAbnormalMessageAction error:" + resourceId + "、" + e.getMessage());
//                }
//            }
        } catch (Exception e) {
            baseBean.writeLog("GradeAbnormalMessageAction error:" + e.getMessage());
        }
        return Action.SUCCESS;
    }
}
